/*
 * Copyright 2009 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.dailydev.wave.robot.spring;

import java.net.HttpURLConnection;
import java.util.Calendar;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.dailydev.wave.robot.IRobot;
import org.dailydev.wave.robot.serializer.CapabilitiesSerializer;
import org.dailydev.wave.robot.serializer.ISerializer;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;

/**
 * @author Roman Bosak, DailyDev.org
 */
public class CapabilitiesController implements Controller {

    /**
     * {@link RobotController}'s logger
     */
    private static Logger log = Logger.getLogger(RobotController.class.getName());

    /**
     * Logger for request and response payload
     */
    private static Logger payloadLog = Logger.getLogger("robot.payload.capabilities");

    private String capabilitiesDocument;

    protected IRobot robot;

    protected ISerializer<IRobot> capabilitiesGenerator = new CapabilitiesSerializer();

    /**
     * 
     */
    public CapabilitiesController() {
    }

    /** {@inheritDoc} */
    @Override
    public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception {
        if (capabilitiesDocument == null) {
            capabilitiesDocument = capabilitiesGenerator.serialize(robot);
        }

        response.setContentType("text/xml");
        response.setCharacterEncoding("utf-8");

        response.setHeader("Cache-Control", "private, max-age=60");
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.SECOND, 60);
        response.setDateHeader("Expires", calendar.getTimeInMillis());

        response.getWriter().write(capabilitiesDocument);
        response.setStatus(HttpURLConnection.HTTP_OK);

        if (payloadLog.isLoggable(Level.FINEST)) {
            payloadLog.finest("RESPONSE: 200 OK : \n" + capabilitiesDocument);
        }

        return null;
    }

    /**
     * @return the robot
     */
    public IRobot getRobot() {
        return robot;
    }

    /**
     * @param robot
     *            the robot to set
     */
    public void setRobot(IRobot robotController) {
        this.robot = robotController;
    }

}
